<template>
    <div>
        <el-button type="primary" plain @click="dialogFormVisible = true">新增放映厅</el-button>
        <el-dialog title="新增放映厅" :visible.sync="dialogFormVisible">
            <el-form :model="form" label-width="100px" :rules="rules" ref="form">
                <el-form-item label="放映厅名称" prop="room_name">
                    <el-input v-model="form.room_name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="放映厅类型" prop="room_type">
                    <el-select v-model="form.room_type" placeholder="请选择放映厅类型">
                        <el-option v-for="item in roomTypes" :key="item.movie_cinema_id" :label="item.type_name"
                            :value="item.type_name">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('form')">确 定</el-button>
            </div>
        </el-dialog>
        <el-divider content-position="left">放映厅列表</el-divider>

        <!-- 放映厅列表 -->
        <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="room_name" label="放映厅名称" width="300">
            </el-table-column>
            <el-table-column prop="room_type" label="放映厅类型" width="300">
            </el-table-column>
            <el-table-column prop="room_size" label="放映厅座位数" width="120">
                <template slot-scope="scope">
                    {{ scope.row.room_size ? scope.row.room_size : '暂未配置座位' }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="success" icon="el-icon-s-operation" circle
                            @click="$router.push(`/home/cinema-room-list/${form.movie_cinema_id}/plan-list/${scope.row.id}`)">
                        </el-button>
                        <el-button type="success" icon="el-icon-plus" circle
                            @click="$router.push(`/home/cinema-room-list/${form.movie_cinema_id}/plan-add/${scope.row.id}`)">
                        </el-button>
                        <el-button type="warning" icon="el-icon-s-grid" circle
                            @click="$router.push(`/home/seat-template/${scope.row.id}`)">
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" circle @click="delCinemaRoom(scope.row.id)">
                        </el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            dialogFormVisible: false,
            form: {
                room_name: '',  //放映厅名称
                room_type: '',  //放映厅类型
                movie_cinema_id: ''  //电影院id
            },
            rules: {
                room_name: [{ required: true, message: '请填写放映厅名称', trigger: 'blur' }],
                room_type: [{ required: true, message: '请选择类型', trigger: 'blur' }]
            },
            roomTypes: [],
            tableData: []
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$http.cinemaRoomApi.add(this.form).then(res => {
                        if (res.data.code == 200) {
                            this.$refs[formName].resetFields();
                            this.$message({
                                message: '新增放映厅成功',
                                type: 'success'
                            });
                            this.$http.cinemaRoomApi.list({ cinema_id: this.$route.params.id }).then(res => {
                                this.tableData = res.data.data
                            });
                        }
                    }).catch(err => {
                        console.log(err);
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
            this.dialogFormVisible = false
        },
        delCinemaRoom(id) {
            this.$confirm('此操作将永久删除此项, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.cinemaRoomApi.delete({ id }).then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.$http.cinemaRoomApi.list({ cinema_id: this.$route.params.id }).then(res => {
                            this.tableData = res.data.data
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    },
    mounted() {
        this.form.movie_cinema_id = this.$route.params.id
        this.$http.cinemaRoomApi.queryAllTypes().then(res => {
            this.roomTypes = res.data.data
        });
        this.$http.cinemaRoomApi.list({ cinema_id: this.$route.params.id }).then(res => {
            this.tableData = res.data.data
        });
    },
}
</script>

<style lang="scss" scoped>

</style>